<?php
include ("../_vars.php");

//error_reporting(-1);
$result_array=array();

$res = mysql_query("SELECT * FROM `adds` ORDER BY `active` DESC , `id` DESC");
echo mysql_error();

$i=0;
while ($row = mysql_fetch_assoc($res) ) {
  $active = $row["active"];
  if ($active == 1 ) {
    $result_array[$i] = array( "id" => $row["id"], "active" => $active, "name" =>$row["name"], "div"=>$row["div"]);
    $i++; 
  }
}
$nrows = $i;

$max_adds = $nrows;
$voted_today = false;
$voted_today_msg = "<h3>Thank you for voting today!</h3>";

// get the user id from the email address
$user_email = $_SESSION["user"];  
// get user id
$result = mysql_query("SELECT id FROM `users` WHERE `email` LIKE '$user_email' ");
$user_id = mysql_result($result, 0);

// get current date
$date = date('Y-m-d');

// check if user voted today
$result = mysql_query("SELECT * FROM `rating` WHERE `user_id` = '$user_id' AND `date` LIKE '$date' ");

$voted_today = true;
if ( mysql_num_rows($result) == 0 )
  $voted_today = false;

if ($voted_today) {
  // if already voted today
  echo $voted_today_msg;
}
else if ($_POST["submitted"] == 1 )  {
  // if voted just now

  // submit all votes to the database  
  for ($i=0; $i<$max_adds; $i++) {
    $rating = $_POST['star-'.$i];
    $add_id = $result_array[$i]["id"];
    mysql_query( "INSERT INTO `tudor`.`rating` (`user_id`, `options`, `add_id`,`date`) VALUES ('$user_id', '$rating', '$add_id', '$date')" );
  }
  
  //
  echo $voted_today_msg;
}
else 
// if not voted today
{
?>
<form id="rating-form" method="post">
<?
  

  for($i=0; $i<$max_adds; $i++)
  {
  // fetch add 
//    fetch_add($i);
  echo "<h4>Add number ".$add_number."</h4>";
  echo "</br></br>";
  echo "Provider: ".$result_array[$i]["name"]."<br/>";
  echo "Div: ".$result_array[$i]["div"];
  echo "<br/><br/>";
  // add ratings for it
  ?>
    <input name='star-<?php echo $i; ?>' type="radio" class="star" value="1"/>
    <input name='star-<?php echo $i; ?>' type="radio" class="star" value="2"/>
    <input name='star-<?php echo $i; ?>' type="radio" class="star" checked="checked" value="3"/>
    <input name='star-<?php echo $i; ?>' type="radio" class="star" value="4"/>
    <input name='star-<?php echo $i; ?>' type="radio" class="star" value="5"/>
  <br/>
<?php
}
?>
  <input type="hidden" name="submitted" value="1" />
  <input type="submit" name="Vote" value="Vote" />
<form />

<?php
}
?>

<script type="text/javascript">

var options = { 
	target: "#stage",   // target element(s) to be updated with server response 
	url: "main.php?action=rating"
				}; 
		  
$("#rating-form").ajaxForm(options);


$('input[type=radio].star').rating();

$('.auto-submit-star').rating({
  callback: function(value, link){
  alert(value);
}
});

function vote(){

  var r=confirm("Cast vote? Once you go black you never go back...");
  if (r==true) {
    // disable inputs
    $('input').rating('disable'); 
//		$('#form1 :radio.star').rating(); 
//    $('#form2 :radio.star').rating();
    // submit the values to the database
   /*

  */
  }

}
</script>


